home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 17 / CU Amiga Magazine's Super CD-ROM 17 (1997)(EMAP Images)(GB)[!][issue 1997-12].iso / CUCD / Magazine / C_Tutorial / Part-3 / gadgets1a.c < prev    next >
C/C++ Source or Header  |  1997-07-27  |  7KB  |  276 lines

  1. #include<exec/libraries.h>
  2. #include<intuition/intuition.h>
  3. #include<utility/tagitem.h>
  4. #include<graphics/text.h>
  5. #include<graphics/rastport.h>
  6. #include<intuition/screens.h>
  7. #include<libraries/gadtools.h>
  8.  
  9. #include<string.h>
  10. #include<stdio.h>
  11.  
  12. #include<clib/exec_protos.h>
  13. #include<clib/graphics_protos.h>
  14. #include<clib/intuition_protos.h>
  15. #include<clib/layers_protos.h>
  16. #include<clib/gadtools_protos.h>
  17.  
  18. /* The library base global variables */
  19. /* (The different style of opening libraries requires these to be initialised to NULL) */
  20. struct Library* GfxBase = NULL;
  21. struct Library* IntuitionBase = NULL;
  22. struct Library* LayersBase = NULL;
  23. struct Library* GadToolsBase = NULL;
  24.  
  25. /* Need to give prototypes for our functions */
  26. void handleIDCMP(struct Window*);
  27. int setClip(struct Window*);
  28. void removeClip(struct Window*);
  29. void setupWindow();
  30. void createWindow(struct Gadget*);
  31. int openLibs();
  32. void closeLibs();
  33.  
  34. /* Some constants for the size of the window */
  35. #define MYWIN_WIDTH        (400)
  36. #define MYWIN_HEIGHT    (200)
  37.  
  38. /* Some constants for the position and size of our gadget */
  39. #define MYGAD_LEFT        (10)
  40. #define MYGAD_TOP            (5)
  41. #define MYGAD_WIDTH        (80)
  42. #define MYGAD_HEIGHT    (12)
  43. #define MYGAD_TEXT        "Next Pen"
  44. #define MYGAD_ID            (0)
  45.  
  46. /* The top gap required around the gadgets */
  47. #define MYTOPGAP      (30)
  48.  
  49. /* The start of the program */
  50. void main()
  51. {
  52.     /* Use a different style of opening libraries... */
  53.     if(openLibs())
  54.     {
  55.         /* Now do the real work */
  56.         setupWindow();
  57.     }
  58.     /* Matched call to close libraries */
  59.     closeLibs();
  60. }
  61.  
  62. /* The open library code is very symmetrical, so we could use a macro: */
  63. #define TryOpenLib(base,lib,vers) \
  64.     if((base = OpenLibrary((lib),(vers))) == NULL) \
  65.     { \
  66.         printf("Error: could not open " lib "\n"); \
  67.         return FALSE; \
  68.     }
  69.  
  70. /* Try to open all the libraries -- return TRUE on success */
  71. int openLibs()
  72. {
  73.     TryOpenLib(GfxBase,                "graphics.library",        37);
  74.     TryOpenLib(IntuitionBase,    "intuition.library",    37);
  75.     TryOpenLib(LayersBase,        "layers.library",            37);
  76.     TryOpenLib(GadToolsBase,    "gadtools.library",        37);
  77.   return TRUE;
  78. }
  79.  
  80. /* Close any open library */
  81. void closeLibs()
  82. {
  83.     if(GadToolsBase)
  84.         CloseLibrary(GadToolsBase);
  85.     if(LayersBase)
  86.         CloseLibrary(LayersBase);
  87.     if(IntuitionBase)
  88.         CloseLibrary(IntuitionBase);
  89.     if(GfxBase)
  90.         CloseLibrary(GfxBase);
  91. }
  92.  
  93. /* Setup the window -- do the GadTools stuff */
  94. void setupWindow()
  95. {
  96.     struct Screen* scr;
  97.     /* We'll copy the visual information for the default public screen */
  98.   /* (usually, this is the Workbench screen) */
  99.     if(scr = LockPubScreen(NULL))
  100.     {
  101.         APTR vinfo;
  102.         /* Get the visual info so GadTools can render the gadgets nicely */
  103.         if(vinfo = GetVisualInfo(scr, TAG_DONE))
  104.         {
  105.             /* We can initialise glist in its declaration */
  106.             struct Gadget* glist = NULL;
  107.             struct Gadget* gad;
  108.             int offtop, offleft;
  109.             struct NewGadget newgad;
  110.             /* Initialised structure declaration: describes 8pt Topaz font */
  111.             struct TextAttr topazFont = { "topaz.font", 8, 0, 0, };
  112.             /* Start a GadTools gadget list */
  113.             /* (Don't need to check gad, since CreateGadget() will fail if gad is NULL) */
  114.             gad = CreateContext(&glist);
  115.             /* The offsets of our window borders */
  116.             offleft = scr->WBorLeft;
  117.             offtop = scr->WBorTop + (scr->Font->ta_YSize + 1);
  118.             /* Setup our first gadget */
  119.             newgad.ng_TextAttr         = &topazFont;
  120.             newgad.ng_VisualInfo     = vinfo;
  121.             newgad.ng_LeftEdge         = MYGAD_LEFT + offleft;
  122.             newgad.ng_TopEdge         = MYGAD_TOP + offtop;
  123.             newgad.ng_Width             = MYGAD_WIDTH;
  124.             newgad.ng_Height             = MYGAD_HEIGHT;
  125.             newgad.ng_GadgetText    = MYGAD_TEXT;
  126.             newgad.ng_GadgetID        = MYGAD_ID;
  127.             newgad.ng_Flags                = 0;
  128.             /* Now create it and add it to our list */
  129.             if(gad = CreateGadget(BUTTON_KIND, gad, &newgad, TAG_DONE))
  130.                 createWindow(glist);
  131.             else
  132.                 printf("Error: could not create gadget(s)\n");
  133.             /* Free the gadget */
  134.             FreeGadgets(glist);
  135.             FreeVisualInfo(vinfo);
  136.         }
  137.         else
  138.             printf("Error: could not get visual info\n");
  139.         UnlockPubScreen(NULL, scr);
  140.     }
  141.     else
  142.         printf("Error: could not lock public screen\n");
  143. }
  144.  
  145. /* Actually open the window, in the normal way */
  146. void createWindow(struct Gadget* glist)
  147. {
  148.     struct Window* win;
  149.     /* Open our window */
  150.     if(win = OpenWindowTags(NULL,
  151.                                                     WA_Width,        MYWIN_WIDTH,
  152.                                                     WA_Height,    MYWIN_HEIGHT,
  153.                                                     WA_Flags,        WFLG_CLOSEGADGET | WFLG_DRAGBAR | WFLG_REPORTMOUSE,
  154.                                                     WA_IDCMP,        IDCMP_CLOSEWINDOW | IDCMP_MOUSEBUTTONS | IDCMP_MOUSEMOVE | BUTTONIDCMP | IDCMP_REFRESHWINDOW,
  155.                                                     WA_Gadgets,    glist,
  156.                                                     TAG_DONE,        0))
  157.     {
  158.         /* If window opened, set clip region */
  159.         if(setClip(win))
  160.         {
  161.             /* Let GadTools refresh its bits of the window */
  162.             GT_RefreshWindow(win, NULL);
  163.             /* Now handle messages */
  164.             handleIDCMP(win);
  165.             removeClip(win);
  166.         }
  167.         else
  168.             printf("Error: could not set clip region on window\n");
  169.         CloseWindow(win);
  170.     }
  171.     else
  172.         printf("Error: could not open window\n");
  173. }
  174.  
  175. /* Our message handling code */
  176. void handleIDCMP(struct Window* win)
  177. {
  178.     char* text = "Hello World!";
  179.     int going = TRUE;
  180.     int drawing = FALSE;
  181.     UBYTE pen = 1;
  182.     SetAPen(win->RPort, pen);
  183.     /* Set the drawing mode to draw only the foreground of text, not the background */
  184.     SetDrMd(win->RPort, JAM1);
  185.     while(going)
  186.     {
  187.         struct IntuiMessage* intuimsg;
  188.         /* Wait for messages to arrive */
  189.         WaitPort(win->UserPort);
  190.         /* Messages have arrived: loop through all of them */
  191.         while(intuimsg = GT_GetIMsg(win->UserPort))
  192.         {
  193.             /* Act on this message... */
  194.             switch(intuimsg->Class)
  195.             {
  196.             case IDCMP_MOUSEBUTTONS:
  197.                 switch(intuimsg->Code)
  198.                 {
  199.                 case SELECTDOWN:
  200.                     drawing = TRUE;
  201.                     break;
  202.                 case SELECTUP:
  203.                     drawing = FALSE;
  204.                     break;
  205.                 }
  206.                 /* break; omitted so we draw on click, too */
  207.             case IDCMP_MOUSEMOVE:
  208.                 /* Don't draw on top gap which holds gadget */
  209.                 if(drawing && intuimsg->MouseY > win->BorderTop+MYTOPGAP)
  210.                 {
  211.                     Move(win->RPort, intuimsg->MouseX, intuimsg->MouseY);
  212.                     Text(win->RPort, text, strlen(text));
  213.                 }
  214.                 break;
  215.             case IDCMP_CLOSEWINDOW:
  216.                 going = FALSE;
  217.                 break;
  218.             case IDCMP_REFRESHWINDOW:
  219.                 /* You *MUST* remember to ask for and handle these refresh messages */
  220.                 GT_BeginRefresh(win);
  221.                 GT_EndRefresh(win, TRUE);
  222.                 break;
  223.             case IDCMP_GADGETUP:
  224.                 /* Our button was clicked!  Set foreground to next pen colour */
  225.                 pen++;
  226.                 SetAPen(win->RPort, pen);
  227.                 break;
  228.             }
  229.             /* Reply when finished with message */
  230.             GT_ReplyIMsg(intuimsg);
  231.         }
  232.     }
  233. }
  234.  
  235. /* Set a clip region on internal part of window */
  236. int setClip(struct Window* win)
  237. {
  238.     /* Make a new region */
  239.     struct Region* reg;
  240.     if(reg = NewRegion())
  241.     {
  242.         /* Make a rectangle that describes the inside of the window */
  243.         struct Rectangle rect;
  244.         rect.MinX = win->BorderLeft;
  245.         rect.MinY = win->BorderTop;
  246.         rect.MaxX = win->Width - win->BorderRight - 1;
  247.         rect.MaxY = win->Height - win->BorderBottom - 1;
  248.         /* Make the region equal to this rectangle */
  249.         if(OrRectRegion(reg, &rect))
  250.         {
  251.             /* Set the clip region on the window's layer */
  252.             InstallClipRegion(win->WLayer, reg);
  253.             /* Say we succeeded */
  254.             return TRUE;
  255.         }
  256.         else
  257.         {
  258.             /* Failed to set region, so delete it */
  259.             DisposeRegion(reg);
  260.         }
  261.     }
  262.     /* If we get this far they we've failed */
  263.     return FALSE;
  264. }
  265.  
  266. /* Remove the clip region from a window */
  267. void removeClip(struct Window* win)
  268. {
  269.     struct Region* reg;
  270.     if(reg = InstallClipRegion(win->WLayer, NULL))
  271.     {
  272.         /* If a clip region is installed it's our new one, so delete it */
  273.         DisposeRegion(reg);
  274.     }
  275. }
  276.